package com.mc.moring.main;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseActivity;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.base.SaveZiLiPageTool;
import com.mc.moring.bean.StationMessageListBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.main.adapter.SellBillAdapter;
import com.mc.moring.main.modle.PermisosModle;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

public class MsgActivity extends BaseActivity {
    SmartRefreshLayout refresh;
    LinearLayout ll_nodata;
    RecyclerView mRecyclerView;
    private SellBillAdapter followAdapter;
    private int pageIndex = 1;
    private ImageButton abBackIBtn;
    private TextView abTitleTV;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logE("msg page");
        PointMgr.INSTANCE.sendEvent("cl_in_app_msg_click");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_in_app_msg_click");
        setContentView(R.layout.activity_msg);
        refresh = findViewById(R.id.swipeLayout);
        mRecyclerView = findViewById(R.id.recycler_view);
        abBackIBtn = findViewById(R.id.abBackIBtn);
        abTitleTV = findViewById(R.id.abTitleTV);
        ll_nodata = findViewById(R.id.ll_nodata);
        abTitleTV.setText("Notificación");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MsgActivity.this));
        followAdapter = new SellBillAdapter();
        mRecyclerView.setAdapter(followAdapter);
        pageIndex = 1;
        loadData(pageIndex);
        abBackIBtn.setOnClickListener(v -> finish());
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh.finishRefresh();
                pageIndex = 1;
                loadData(pageIndex);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refresh.finishLoadMore();
                pageIndex++;
                loadData(pageIndex);
            }
        });
    }

    @Override
    public Activity getActivity() {
        return null;
    }

    @Override
    public void initView() {

    }

    private void loadData(int pageIndex) {
        ZiLiPageUserDataBean userinfo = new Gson().fromJson(new SaveZiLiPageTool(MsgActivity.this).getKeyValue("user_info"), ZiLiPageUserDataBean.class);
        Map<String, String> map = new HashMap<>();
        map.put("customerId", userinfo.getCustomerId());
        map.put("pageIndex", pageIndex + "");
        map.put("pageSize", 20 + "");
        showLoading();
        PermisosModle.Companion.getInstant().getStationMessageList(map, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                LogUtils.logE("msg list>" + data);
                StationMessageListBean bean = new Gson().fromJson(data, StationMessageListBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (bean.getCode().equals("0000")) {
                            if (pageIndex == 1 && bean.getData().getList().size() == 0) {
                                ll_nodata.setVisibility(View.VISIBLE);
                                refresh.setVisibility(View.GONE);
                                return;
                            }
                            ll_nodata.setVisibility(View.GONE);
                            refresh.setVisibility(View.VISIBLE);

                            if (pageIndex == 1) {
                                followAdapter.replaceData(bean.getData().getList());
                            } else {
                                followAdapter.addData(bean.getData().getList());
                            }
                        } else {
                            ToastUtils.showShort(MsgActivity.this, bean.getMsg());
                        }
                    }
                });
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                LogUtils.logE("msg fail>" + e);
                hideLoading();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onStop(){
        super.onStop();
        hideLoading();
    }
}
